package com.iseastar.guojiang.transfer;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.iseastar.guojiang.app.Intents;
import com.iseastar.guojiang.model.ParcelTransferPointBean;
import com.shouhuobao.station.R;
import com.shouhuobao.take.utils.StringUtils;

import java.util.ArrayList;

import droid.frame.activity.base.BaseAdapterExt;

/**
 * Created by joker on 2018/1/29.
 * 包裹交接点列表 适配器
 */
public class ParcelTransferListAdapter extends BaseAdapterExt<ParcelTransferPointBean> {

    public ParcelTransferListAdapter(ArrayList<ParcelTransferPointBean> items, Activity context) {
        super(items, context);
        setEmptyView(R.layout.layout_courier_tansfer_parcel_list_empty);
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if (hasEmptyView()) {
            return super.getView(position, convertView, parent);
        }
        ViewHolder holder;
        if (convertView == null || convertView.getTag() == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.transfer_parcel_list_item, parent, false);
            holder = new ViewHolder();
            holder.defaultTransferTV = (TextView) convertView.findViewById(R.id.default_transfer_tv);
            holder.distanceTV = (TextView) convertView.findViewById(R.id.distance_tv);
            holder.transferNameTV = (TextView) convertView.findViewById(R.id.transfer_name_tv);
            holder.transferAddressTV = (TextView) convertView.findViewById(R.id.transfer_address_tv);
            holder.transferTimeTV = (TextView) convertView.findViewById(R.id.transfer_time_tv);
            holder.transferTipsTV = (TextView) convertView.findViewById(R.id.transfer_time_tip_tv);
            holder.contractNameTV = (TextView) convertView.findViewById(R.id.sender_name_tv);
            holder.contractPhoneIV = (ImageView) convertView.findViewById(R.id.sender_phone_tv);
            holder.setDefaultTransferTV = (TextView) convertView.findViewById(R.id.set_default_tansfer_tv);
            holder.navigationTV = (TextView) convertView.findViewById(R.id.navigation_tv);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final ParcelTransferPointBean item = getItem(position);
        if (item.isDefaultWareHouse()) {
            holder.defaultTransferTV.setVisibility(View.VISIBLE);
            holder.setDefaultTransferTV.setVisibility(View.GONE);
        } else {
            holder.defaultTransferTV.setVisibility(View.INVISIBLE);
            holder.setDefaultTransferTV.setVisibility(View.VISIBLE);
        }
        holder.distanceTV.setText("距您" + item.getDistance() + "米");
        holder.transferNameTV.setText(item.getWareHouseName());
        holder.transferAddressTV.setText(item.getAddress());
        holder.transferTimeTV.setText(item.getTransStart() + "-" + item.getTransEnd());
        if (!StringUtils.isEmpty(item.getTimeMessage())) {
            holder.transferTipsTV.setText("(" + item.getTimeMessage() + ")");
        }
        holder.contractNameTV.setText("联系人：" + item.getCourierNickName());
        holder.contractPhoneIV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intents.dialOrCall(item.getCourierPhone());
            }
        });
        holder.setDefaultTransferTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ((ParcelTransferListActivity) context).setDefaultTransferPoint(position, item);
            }
        });
        holder.navigationTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ((ParcelTransferListActivity) context).navigation(item);
            }
        });
        return convertView;
    }

    class ViewHolder {
        TextView defaultTransferTV;//默认交接点
        TextView distanceTV;//
        TextView transferNameTV;
        TextView transferAddressTV;
        TextView transferTimeTV;//交接时间
        TextView transferTipsTV;
        TextView contractNameTV;//联系人
        ImageView contractPhoneIV;//联系人电话
        TextView setDefaultTransferTV;//设为默认交接点
        TextView navigationTV;//导航
    }

}
